package edu.fit5046.mymonashmate.activity;

//final view
import java.util.concurrent.ExecutionException;

import org.json.JSONException;

import edu.fit5046.mymonashmate.R;
import edu.fit5046.mymonashmate.utils.HttpUtils;
import edu.fit5046.mymonashmate.utils.SimpleInfo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {
	public static final String TAG = "Login";
	public static final String url = "/entity.pin";
	public static String acc = null;
	public static String pin = null;

	EditText stuid;
	EditText code;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_login);
		Button loginButton = (Button) findViewById(R.id.button_login_login);
		Button registerButton = (Button) findViewById(R.id.button_login_register);
		loginButton.setOnClickListener(this);
		registerButton.setOnClickListener(this);
		stuid = (EditText) findViewById(R.id.text_login_user);
		code = (EditText) findViewById(R.id.text_login_password);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.button_login_login:
			try {
				acc = stuid.getText().toString();
				pin = code.getText().toString();
				login(acc, pin);
			} catch (InterruptedException | ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;

		case R.id.button_login_register:
			register();
			break;
		default:
			break;
		}

	}

	@SuppressLint("ShowToast")
	private void login(String acc, String pin) throws InterruptedException,
			ExecutionException, JSONException {
		if ((acc.length() < 3) || (pin.length() < 2)) {
			Toast.makeText(this, "Ivailid Input", Toast.LENGTH_LONG).show();
		} else {

			String result = new MyTask().execute(
					url + "/CheckAccPin/"+acc+"/"+pin).get();
			Toast.makeText(this, result, Toast.LENGTH_LONG).show();
			if (result.contains("success")) {
				SimpleInfo.localId=Integer.parseInt(acc);
				Intent intent = new Intent();
				intent.setClass(this, MapActivity.class);
				startActivity(intent);
				finish();
			} 
		}
	}
	private void register() {
		Intent intent = new Intent();
		intent.setClass(this, RegisterActivity.class);
		startActivity(intent);
	}
	
	private class MyTask extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... prm) {
			// TODO Auto-generated method stub

			return HttpUtils.Get(prm[0]);
		}

		protected void onPostExecute(String result) {
			super.onPostExecute(result);
		}

	}
}
